#encoding=utf-8

import urllib2
'''
def getPageSource(url):
    try:
        req = urllib2.Request(url)
        req.add_header('User-agent', 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)')
        response = urllib2.urlopen(req)

#            response = urllib2.urlopen(url)
            
        page=response.read()
        page=page.decode('gb18030').encode('utf-8')
        except urllib2.HTTPError,e:
            print "Http error: "+str(e.code)
            return [str(e.code),None]
        except urllib2.URLError,e:
            print "URL open error: "+str(e.reason[0]))
            return [str(e.reason[0]),None]
        except Exception,e:
            print "Error: "+str(e)
            return [str(e),None]
        return ["200",page]
'''



class DownSource :
    def __init__(self, url):   
        self.__url = url
        
    def get(self): 
    	try:
    		req = urllib2.Request(self.__url)  
    		req.add_header('User-agent', 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)')
    		response = urllib2.urlopen(req)
    		source = response.read()
    		if source == "" :
    			raise ValueError("Nothing get from : " + self.__url)
    		coding = response.headers['Content-Type'].split('=')[1]
    		if coding :
    			source = source.decode(coding).encode('utf-8')
    		return source
    	except Exception,e :
    		print e
    		return "Exception"

    		

